package com.jstyle.jclife.adapter;

import android.content.Context;
import android.graphics.Color;
import android.location.Location;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.amap.api.maps.model.LatLng;
import com.jstyle.blesdk.model.ExerciseMode;
import com.jstyle.jclife.R;
import com.jstyle.jclife.model.ItemData;
import com.jstyle.jclife.model.PathRecord;
import com.jstyle.jclife.utils.DateUtil;
import com.jstyle.jclife.utils.ResolveData;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class TrackRecordDetailAdapter extends RecyclerView.Adapter<BaseViewHolder> {
    private String calLabel;
    private String heartLabel;
    private String durationLabel;
    private String stepLabel;
    private String hzLabel;
    private String distanceLabel;
    private String speedLabel;
    private String paceLabel;
    private Context mContext;

    private String calUnit;
    private String heartUnit;
    private String durationUnit;
    private String stepUnit;
    private String hzUnit;
    private String distanceUnit;
    private String speedUnit;
    private String paceUnit;
    PathRecord pathRecord;

    List<LatLng> mLatLngList;
    int heart;
    List<com.google.android.gms.maps.model.LatLng> mLatLngListGoogle;

    public TrackRecordDetailAdapter(Context context, PathRecord pathRecord, List<LatLng> mLatLngList, int heart) {
        mContext = context;
        this.pathRecord = pathRecord;
        this.mLatLngList = mLatLngList;
        this.heart=heart;
        init();
    }
    public TrackRecordDetailAdapter(List<com.google.android.gms.maps.model.LatLng> mLatLngList, Context context, PathRecord pathRecord, int heart) {
        mContext = context;
        this.pathRecord = pathRecord;
        this.mLatLngListGoogle = mLatLngList;
        this.heart=heart;
        init();
    }


    private void init(){
        heartLabel = mContext.getString(R.string.avg_hr_unit);
        heartUnit=mContext.getString(R.string.hr_laber);
        durationLabel = mContext.getString(R.string.Active_Minutes);
        stepLabel = mContext.getString(R.string.Step);
        stepUnit = mContext.getString(R.string.Steps_unit);

        paceLabel = mContext.getString(R.string.Average_pace_content);
        paceUnit = ResolveData.getPaceLabel(mContext);

        speedLabel = mContext.getString(R.string.Average_speed_content);
        speedUnit = ResolveData.getSpeedLabel(mContext);

        distanceLabel = mContext.getString(R.string.Distance);
        distanceUnit = ResolveData.getDistanceLabel(mContext);

        hzLabel = mContext.getString(R.string.Average_Stride);
        hzUnit=mContext.getString(R.string.Stride_label);

        calLabel = mContext.getString(R.string.cal_unit);
        calUnit=mContext.getString(R.string.cal_laber);
    }
    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {

            case ItemData.ITEM_TYPE_RUN:
                view = LayoutInflater.from(mContext).inflate(
                        R.layout.item_detail_run, parent, false);
                return new ExerciseRecordHistoryViewHolder(view);
            case ItemData.ITEM_TYPE_BIKING:
                view = LayoutInflater.from(mContext).inflate(
                        R.layout.item_detail_bike, parent, false);
                return new ExerciseRecordBikingViewHolder(view);
            case ItemData.ITEM_TYPE_HIKING:
                view = LayoutInflater.from(mContext).inflate(
                        R.layout.item_detail_hiking, parent, false);
                return new ExerciseRecordHikingViewHolder(view);
            case ItemData.ITEM_TYPE_OTHER:
                view = LayoutInflater.from(mContext).inflate(
                        R.layout.item_detail_other, parent, false);
                return new ExerciseRecordOtherViewHolder(view);
            default:
                view = LayoutInflater.from(mContext).inflate(
                        R.layout.item_detail_other, parent, false);
                return new ExerciseRecordOtherViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        switch (getItemViewType(position)) {

            case ItemData.ITEM_TYPE_RUN:
                bindRun(holder, position);
                break;
            case ItemData.ITEM_TYPE_BIKING:
                bindBIKING(holder, position);
                break;
            case ItemData.ITEM_TYPE_HIKING:
                bindHiking(holder, position);
                break;
            case ItemData.ITEM_TYPE_OTHER:
                bindOther(holder, position);
                break;
            default:
                break;
        }
    }


    @Override
    public int getItemCount() {
        return 1;
    }






    @Override
    public int getItemViewType(int position) {
        int mode = pathRecord.getExerciseMode();
        int viewType = 0;
        switch (mode) {
            case ExerciseMode.Mode_RUN:
                viewType = ItemData.ITEM_TYPE_RUN;
                break;
            case ExerciseMode.Mode_CYCLING:
                viewType = ItemData.ITEM_TYPE_BIKING;
                break;
            case ExerciseMode.Mode_CLIMB:
                viewType = ItemData.ITEM_TYPE_HIKING;
                break;
            default:
                viewType = ItemData.ITEM_TYPE_OTHER;
                break;
        }
        return viewType;
    }


    private Spannable getSpannable(String text, String label) {
        int start = text.indexOf(label);
        int end = start + label.length();
        Spannable kcalSpannable = new SpannableString(text);
        setColorSpannable(kcalSpannable, start, end, Color.parseColor("#86878C"));
        return kcalSpannable;
    }

    private void setColorSpannable(Spannable word, int start, int end, int textColor) {
        word.setSpan(new ForegroundColorSpan(textColor), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
    }

    private void initGpsData(final int time, final Button btSpeed, final Button btDistance, PathRecord pathRecord) {
        float distance = Float.parseFloat(pathRecord.getMDistance());
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(2);
        String showDistance= ResolveData.getShowDistance(String.valueOf(distance));
        float speed = Float.valueOf(showDistance) * 3600f / time;
        String speedString = numberFormat.format(speed) +" "+speedUnit+ "\n" + speedLabel;
        String distanceString = showDistance +" "+ distanceUnit+"\n" + distanceLabel;
        btSpeed.setText(getSpannable(speedString, speedLabel));
        btDistance.setText(getSpannable(distanceString, distanceLabel));
    }


    private void bindOther(BaseViewHolder holder, int position) {
        ExerciseRecordOtherViewHolder viewHolder = (ExerciseRecordOtherViewHolder) holder;
        final PathRecord pathRecord = this.pathRecord;
        String heart = pathRecord.getHeart();
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(2);
        numberFormat.setMinimumFractionDigits(2);
        String cal = pathRecord.getCalString() + "\n" + calLabel;
        String duration = DateUtil.getSportTime(Integer.parseInt(pathRecord.getMDuration())) + "\n" + durationLabel;
        String heartString = this.heart + "\n" + heartLabel;
        viewHolder.buttonExerciseCal.setText(getSpannable(cal, calLabel));
        viewHolder.buttonExerciseDurtuion.setText(getSpannable(duration, durationLabel));
        viewHolder.buttonHeart.setText(getSpannable(heartString, heartLabel));

    }

    private void bindHiking(BaseViewHolder holder, int position) {
        ExerciseRecordHikingViewHolder viewHolder = (ExerciseRecordHikingViewHolder) holder;
        PathRecord pathRecord = this.pathRecord;
        String heart = pathRecord.getHeart();
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(2);
        String showDistance = pathRecord.getMDistance();

        String cal = pathRecord.getCalString() + " "+speedUnit+"\n" + calLabel;
        String duration = DateUtil.getSportTime(Integer.parseInt(pathRecord.getMDuration())) + "\n" + durationLabel;
        String heartString = this.heart + " "+heartUnit+"\n" + heartLabel;
        String distanceString = showDistance + "\n" + distanceLabel;
        String step = pathRecord.getMStep() + " "+stepUnit+"\n" + stepLabel;
        int time = Integer.valueOf(pathRecord.getMDuration());
        viewHolder.buttonExerciseStep.setText(getSpannable(step, stepLabel));
        viewHolder.buttonExerciseCal.setText(getSpannable(cal, calLabel));
        viewHolder.buttonExerciseDurtuion.setText(getSpannable(duration, durationLabel));
        viewHolder.buttonHeart.setText(getSpannable(heartString, heartLabel));
        viewHolder.buttonExerciseDistance.setText(getSpannable(distanceString, distanceLabel));
        initGpsData(time, viewHolder.buttonExerciseSpeed, viewHolder.buttonExerciseDistance,pathRecord);

    }

    private void bindBIKING(BaseViewHolder holder, int position) {
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(2);
        ExerciseRecordBikingViewHolder viewHolder = (ExerciseRecordBikingViewHolder) holder;
        PathRecord pathRecord = this.pathRecord;
        String heart = pathRecord.getHeart();
        String showDistance = null;
        try {
            showDistance = numberFormat.parse(pathRecord.getMDistance()).toString();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String cal = pathRecord.getCalString() + " "+calUnit+"\n" + calLabel;
        String duration = DateUtil.getSportTime(Integer.parseInt(pathRecord.getMDuration())) + "\n" + durationLabel;
        String heartString =this. heart + " "+heartUnit+"\n" + heartLabel;
        String distanceString = showDistance + "\n" + distanceLabel;
        int time = Integer.valueOf(pathRecord.getMDuration());
        viewHolder.buttonExerciseCal.setText(getSpannable(cal, calLabel));
        viewHolder.buttonExerciseDurtuion.setText(getSpannable(duration, durationLabel));
        viewHolder.buttonHeart.setText(getSpannable(heartString, heartLabel));
        viewHolder.buttonExerciseDistance.setText(getSpannable(distanceString, distanceLabel));
        initGpsData(time, viewHolder.buttonExerciseSpeed, viewHolder.buttonExerciseDistance,pathRecord);
    }
    public void setHeart(int heart){
        this.heart=heart;
        notifyDataSetChanged();
    }
    private void bindRun(BaseViewHolder holder, int position) {
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMaximumFractionDigits(2);
        ExerciseRecordHistoryViewHolder viewHolder = (ExerciseRecordHistoryViewHolder) holder;
        PathRecord pathRecord = this.pathRecord;
        //String heart = pathRecord.getHeartStatus();
        String cal = pathRecord.getCalString() +" "+calUnit+ "\n" + calLabel;

        String pace = ResolveData.getShowPace(mContext, pathRecord.getMAveragespeed()) + " "+paceUnit+"\n" + paceLabel;
        String heartString = this.heart + " "+heartUnit+"\n" + heartLabel;

        int time = Integer.valueOf(pathRecord.getMDuration());
        String step = pathRecord.getMStep();
        int stepM = (int) Math.rint(Double.valueOf(step) * 60 / time);
        String duration = DateUtil.getSportTime(time) + "\n" + durationLabel;
        String stepHz = stepM + " "+hzUnit+"\n" + hzLabel;
        String stepString = step +" "+ stepUnit+ "\n" + stepLabel;
        viewHolder.buttonExerciseStep.setText(getSpannable(stepString, stepLabel));
        viewHolder.buttonExerciseCal.setText(getSpannable(cal, calLabel));
        viewHolder.buttonExerciseDurtuion.setText(getSpannable(duration, durationLabel));
        viewHolder.buttonHeart.setText(getSpannable(heartString, heartLabel));
        viewHolder.buttonExerciseSpace.setText(getSpannable(pace, paceLabel));
        viewHolder.buttonExerciseStephz.setText(getSpannable(stepHz, hzLabel));
        initGpsData(time, viewHolder.buttonExerciseSpeed, viewHolder.buttonExerciseDistance,pathRecord);
    }

    private static final String TAG = "ExerciseRecordHistoryAd";


    public class ExerciseRecordHistoryViewHolder extends BaseViewHolder {//跑步


        @BindView(R.id.button_exercise_durtuion)
        Button buttonExerciseDurtuion;
        @BindView(R.id.button_exercise_cal)
        Button buttonExerciseCal;
        @BindView(R.id.button_exercise_distance)
        Button buttonExerciseDistance;
        @BindView(R.id.button_exercise_speed)
        Button buttonExerciseSpeed;
        @BindView(R.id.button_exercise_space)
        Button buttonExerciseSpace;
        @BindView(R.id.button_exercise_step)
        Button buttonExerciseStep;
        @BindView(R.id.button_exercise_stephz)
        Button buttonExerciseStephz;
        @BindView(R.id.button_heart)
        Button buttonHeart;

        public ExerciseRecordHistoryViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ExerciseRecordOtherViewHolder extends BaseViewHolder {//其他剩下的

        @BindView(R.id.button_exercise_durtuion)
        Button buttonExerciseDurtuion;
        @BindView(R.id.button_exercise_cal)
        Button buttonExerciseCal;
        @BindView(R.id.button_heart)
        Button buttonHeart;

        public ExerciseRecordOtherViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ExerciseRecordHikingViewHolder extends BaseViewHolder {

        @BindView(R.id.button_exercise_durtuion)
        Button buttonExerciseDurtuion;
        @BindView(R.id.button_exercise_cal)
        Button buttonExerciseCal;
        @BindView(R.id.button_exercise_distance)
        Button buttonExerciseDistance;
        @BindView(R.id.button_exercise_speed)
        Button buttonExerciseSpeed;
        @BindView(R.id.button_exercise_step)
        Button buttonExerciseStep;
        @BindView(R.id.button_heart)
        Button buttonHeart;

        public ExerciseRecordHikingViewHolder(View itemView) {//远足
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class ExerciseRecordBikingViewHolder extends BaseViewHolder {//骑车



        @BindView(R.id.button_exercise_durtuion)
        Button buttonExerciseDurtuion;
        @BindView(R.id.button_exercise_cal)
        Button buttonExerciseCal;
        @BindView(R.id.button_exercise_distance)
        Button buttonExerciseDistance;
        @BindView(R.id.button_exercise_speed)
        Button buttonExerciseSpeed;
        @BindView(R.id.button_heart)
        Button buttonHeart;

        public ExerciseRecordBikingViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

//    private float getDistance(List<LatLng> list,PathRecord pathRecord) {
//        float distance = 0;
//        if (list == null || list.size() == 0) {
//            return Float.valueOf(pathRecord.getMDistance());
//        }
//        for (int i = 0; i < list.size() - 1; i++) {
//            LatLng firstLatLng = list.get(i);
//            LatLng secondLatLng = list.get(i + 1);
//            double betweenDis = AMapUtils.calculateLineDistance(firstLatLng,
//                    secondLatLng);
//            distance = (float) (distance + betweenDis);
//        }
//
//        return distance / 1000f;
//    }

    private float getDistanceGoogle(List<com.google.android.gms.maps.model.LatLng>list, PathRecord pathRecord){
        float distance = 0;
        if (list == null || list.size() == 0) {
            return Float.valueOf(pathRecord.getMDistance());
        }
        for (int i = 0; i < list.size() - 1; i++) {
            com.google.android.gms.maps.model.LatLng firstLatLng = list.get(i);
            com.google.android.gms.maps.model.LatLng secondLatLng = list.get(i + 1);
            float[] results = new float[3];
            Location.distanceBetween(firstLatLng.latitude, firstLatLng.longitude, secondLatLng.latitude, secondLatLng.longitude, results);
            double betweenDis = results[0];
            distance = (float) (distance + betweenDis);
        }
        return distance / 1000f;
    }
}
